package frog.whacker;

import java.util.ArrayList;
import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class FrogManager extends Thread {
    private Panel mPanel;
    private SurfaceHolder mHolder;
    private boolean mRun = false;
	private long mRoundTime;
	private ArrayList<Frogs> mFrogs = new ArrayList<Frogs>();
    
    public FrogManager(Panel panel) {
        mPanel = panel;
        mHolder = mPanel.getHolder();
    }
    
    public void setRunning(boolean run) {
        mRun = run;
    }
    
    @Override
    
    //Continuous game mode
    public void run() 
    {
    	boolean setup = true;
	    while (mRun) {
	    	int ctotal =0;
	        if (setup)
	        {
		    	//generate first round of frogs
	        	Canvas canvas = null;
		        Random rand = new Random();
		        int NFrogs = rand.nextInt(5);
		        mRoundTime = (long) ( 500*((rand.nextDouble() * NFrogs)));
		        mFrogs.clear();
		        for (int i = 0; i<NFrogs;i++)
		        {
		        	double Xpos = rand.nextDouble()*mPanel.getWidth();
		        	double Ypos = rand.nextDouble()*mPanel.getHeight();
		        	ctotal = ctotal + 10;
		        	Bitmap mBitmap = BitmapFactory.decodeResource(mPanel.getResources(), R.drawable.greenfrog);
		        	mFrogs.add(new Frogs(Xpos, Ypos, mBitmap));
		        	mFrogs.add(new Frogs(Xpos, Ypos, mBitmap));
		        }
		        NFrogs = rand.nextInt(5);
		        for (int i = 0; i<NFrogs;i++)
		        {
		        	double Xpos = rand.nextDouble()*mPanel.getWidth();
		        	double Ypos = rand.nextDouble()*mPanel.getHeight();
		        	ctotal = ctotal + 10;
		        	Bitmap mBitmap = BitmapFactory.decodeResource(mPanel.getResources(), R.drawable.redfrog);
		        	mFrogs.add(new Frogs(Xpos, Ypos, mBitmap));
		        	mFrogs.add(new Frogs(Xpos, Ypos, mBitmap));
		        }
		        canvas = mHolder.lockCanvas();
		        if (canvas != null) {
		            
		        	mPanel.setTotalScore(mPanel.getTotalScore()+ctotal);
		        	mPanel.doDraw(mFrogs, canvas);
		            mHolder.unlockCanvasAndPost(canvas);
		        }	
		        setup = false;
	        }
	        else
	        {
		    	//generate next round frogs
	        	Canvas canvas = null;
		        Random rand = new Random();
		        int NFrogs = rand.nextInt(4);
		        mFrogs.clear();
		        for (int i = 0; i<NFrogs;i++)
		        {
		        	double Xpos = rand.nextDouble()*mPanel.getWidth();
		        	double Ypos = rand.nextDouble()*mPanel.getHeight();
		        	ctotal = ctotal + 10;
		        	Bitmap mBitmap = BitmapFactory.decodeResource(mPanel.getResources(), R.drawable.greenfrog);
		        	mFrogs.add(new Frogs(Xpos, Ypos, mBitmap));
		        	mFrogs.add(new Frogs(Xpos, Ypos, mBitmap));
		        }
		        NFrogs = rand.nextInt(4);
		        for (int i = 0; i<NFrogs;i++)
		        {
		        	double Xpos = rand.nextDouble()*mPanel.getWidth();
		        	double Ypos = rand.nextDouble()*mPanel.getHeight();
		        	ctotal = ctotal + 10;
		        	Bitmap mBitmap = BitmapFactory.decodeResource(mPanel.getResources(), R.drawable.redfrog);
		        	mFrogs.add(new Frogs(Xpos, Ypos, mBitmap));
		        	mFrogs.add(new Frogs(Xpos, Ypos, mBitmap));
		        }
		      
		        //wait till next round
		        try {
					this.sleep(mRoundTime);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//print to screen
				canvas = mHolder.lockCanvas();
		        if (canvas != null) {
		            
		        	while(mPanel.mPost){
		        	}
			        	mPanel.mPost = true;
		        		mPanel.setTotalScore(mPanel.getTotalScore()+ctotal);
			        	mPanel.doDraw(mFrogs, canvas);
			            mHolder.unlockCanvasAndPost(canvas);
			            mPanel.mPost = false;
		        }
		  
		        //generate time for this round
		        mRoundTime = (long) ( 1000*(2.5 + (rand.nextDouble() * (NFrogs+1))));
	        }

	        
	    }
    }
    
    //not used yet this will be for different game mode
    /*public void run(int rounds) 
    {
	    for (int k = 0; k < rounds; k++) {
	        Canvas canvas = null;
	        Random rand = new Random();
	        int NFrogs = rand.nextInt(5);
	        mRoundTime = (long) ( 1000*(2.5 + (rand.nextDouble() * (NFrogs+1))));
	        mGFrogs.clear();
	        for (int i = 0; i<NFrogs;i++)
	        {
	        	double Xpos = rand.nextDouble()*mPanel.getWidth();
	        	double Ypos = rand.nextDouble()*mPanel.getHeight();
	        	
	        	mPanel.setTotalScore(mPanel.getTotalScore() + 10);
	    		mGFrogs.add(new GFrog(mPanel.getResources(), Xpos, Ypos));
	    		mGFrogs.add(new GFrog(mPanel.getResources(), Xpos, Ypos));
	        }
	        canvas = mHolder.lockCanvas();
	        if (canvas != null) {
	            
	            mPanel.doDraw(mGFrogs, canvas);
	            mHolder.unlockCanvasAndPost(canvas);
	        }
	        try {
				this.sleep(mRoundTime);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
	    }
    }*/
}
        
        
       